#include "date.h"

inline void Date::SetYear(int year){
    year_ = year;
}

inline void Date::SetMonth(int month){
    month_ = month;
}

inline void Date::SetDay(int day){
    day_ = day;
}

inline int Date::GetDay(){
    return day_;
}

inline int Date::GetMonth(){
    return month_;
}

inline int Date::GetYear(){
    return year_;
}

int Date::DaysInterval(Date& date){
    int d = day_ - date.GetDay();
    int dm = 0;
    int i = month_ < date.GetMonth() ? month_ : date.GetMonth();
    int j = month_ > date.GetMonth() ? month_ : date.GetMonth();
    while(i < j){
        dm += daym[i];
        i ++;
    }
    if(month_ < date.GetMonth()){
        dm = -dm;
    }
    int gain = 0;
    if(year_ < date.GetYear()){
        year_ -= CmpPrim();
        gain = PrimeGain(date.GetYear()+date.CmpPrim());
        year_ += CmpPrim();
    } else {
        year_ += CmpPrim();
        gain = PrimeGain(date.GetYear()-date.CmpPrim());
        year_ -= CmpPrim();
    }
    d = d+dm+365*(year_ - date.GetYear()) + gain;
    if(d<0)d = -d;
    return d;
}

int Date::PrimeGain(int y){
    int gain = 0;
    int i = year_ < y ? year_ : y;
    int j = year_ > y ? year_ : y;
    while(i <= j){
        if(i%4==0 && i%100!=0 || i % 400==0){
            gain++;
        }
        i++;
    }
    return gain;
}

int Date::CmpPrim(){
    if(month_ > 2) 
        return 1;
    else if(month_ < 2) 
        return -1;
    else if (day_ < 29)
        return -1;
    else
        return 1;
}

Date::Date(){};

Date::Date(int y, int m, int d):year_(y), month_(m), day_(d){};
